Before we start:
Start by importing ChemSpider:
In [1]:
from chemspipy import ChemSpider
Then connect to ChemSpider by creating a ChemSpider instance using your security token:
In [2]:
# Tip: Store your security token as an environment variable to reduce the chance of accidentally sharing it
import os
mytoken = os.environ['CHEMSPIDER_SECURITY_TOKEN']
cs = ChemSpider(security_token=mytoken)
All your interaction with the ChemSpider database should now happen through this ChemSpider object, cs
.
Retrieving information about a specific Compound in the ChemSpider database is simple.
Let’s get the Compound with ChemSpider ID 2157:
In [3]:
comp = cs.get_compound(2157)
comp
Out[3]:
Now we have a Compound object called comp
. We can get various identifiers and calculated properties from this object:
In [4]:
print(comp.molecular_formula)
print(comp.molecular_weight)
print(comp.smiles)
print(comp.common_name)
In [5]:
for result in cs.search('glucose'):
print(result)
The search method accepts any identifer that ChemSpider can interpret, including names, registry numbers, SMILES and InChI.